package com.tanhua.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.service.AnalysisService;
import com.tanhua.admin.service.DashboardService;
import com.tanhua.model.domian.Analysis;
import com.tanhua.model.vo.AnalysisSummaryVo;
import com.tanhua.model.vo.ThisLastYearVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
@Slf4j
public class DashboardController {

    @Autowired
    private AnalysisService analysisService;

    @Autowired
    private AnalysisMapper analysisMapper;

    @Autowired
    private DashboardService dashboardService;
    /**
     * 概要统计信息
     */
    @GetMapping("/summary")
    public AnalysisSummaryVo getSummary() {

        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

        Date now = Calendar.getInstance().getTime();

        //查询今日统计信息
        Analysis today_analysis = analysisService.querySummary(now);

        //累计用户数
        Integer total = analysisService.queryCumulativeUsers();
        analysisSummaryVo.setCumulativeUsers(Long.valueOf(total));

        //过去30天活跃用户
        Long month = 0L;
        List<Analysis> queryMonthActive = analysisMapper.queryMonthActive();
        for (Analysis analysis : queryMonthActive) {
            System.out.println(analysis);
            Long aLong = Long.valueOf(analysis.getNumActive());
            month = aLong +month;
        }
        analysisSummaryVo.setActivePassMonth(month);//(Long.valueOf(today_analysis.getNumActive30()));

        //过去7天活跃用户
        Long week = 0L;
        List<Analysis> queryWeekActive = analysisMapper.queryWeekActive();
        for (Analysis analysis : queryWeekActive) {
            System.out.println(analysis);
            Long aLong = Long.valueOf(analysis.getNumActive());
            week = aLong +week;
        }
        analysisSummaryVo.setActivePassWeek(week);//(Long.valueOf(today_analysis.getNumActive7()));

        //今日新增用户
        analysisSummaryVo.setNewUsersToday(Long.valueOf(today_analysis.getNumRegistered()));


        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        //查询昨日统计信息
        Analysis yes_analysis = analysisService.querySummary(DateUtils.addDays(now, -1));

        analysisSummaryVo.setNewUsersTodayRate(computeRate(Long.valueOf(today_analysis.getNumRegistered()), Long.valueOf(yes_analysis.getNumRegistered())));

        //今日登录次数
        analysisSummaryVo.setLoginTimesToday(Long.valueOf(today_analysis.getNumLogin()));

        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setLoginTimesTodayRate(computeRate(Long.valueOf(today_analysis.getNumLogin()), Long.valueOf(yes_analysis.getNumLogin())));

        //今日活跃用户
        analysisSummaryVo.setActiveUsersToday(Long.valueOf(today_analysis.getNumActive()));

        //活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setActiveUsersTodayRate(computeRate(Long.valueOf(today_analysis.getNumActive()), Long.valueOf(yes_analysis.getNumActive())));

        return analysisSummaryVo;
    }

    private static BigDecimal computeRate(Long current, Long last) {
        BigDecimal result;
        if (last == 0) {
            // 当上一期计数为零时，此时环比增长为倍数增长
            result = new BigDecimal((current - last) * 100);
        } else {
            result = BigDecimal.valueOf((current - last) * 100).divide(BigDecimal.valueOf(last)
                    , 2, BigDecimal.ROUND_HALF_DOWN);
        }
        return result;
    }

    private static String offsetDay(Date date, int offSet) {
        return DateUtil.offsetDay(date, offSet).toDateStr();
    }

    @GetMapping("/users")
    public ResponseEntity UserYearDateS(@RequestParam Map map){
        /*ThisLastYearVo responseEntity = new ThisLastYearVo();
        ArrayList<UserYearData> userYearData1 = new ArrayList<>();
        ArrayList<UserYearData> userYearData2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            UserYearData data = new UserYearData();
            data.setAmount(30);
            data.setTitle("2022-01-10");
            userYearData1.add(data);
        }
        responseEntity.setThisYear(userYearData1);
        for (int i = 0; i < 5; i++) {
            UserYearData data = new UserYearData();
            data.setAmount(20);
            data.setTitle("2021-01-11");
            userYearData2.add(data);
        }
        responseEntity.setLastYear(userYearData2);*/
        log.info("map={}",map);
        ThisLastYearVo userYearDateS = dashboardService.UserYearDateS(map);;
        return ResponseEntity.ok(userYearDateS);
    }

}